﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Travel.MVC
{
    /// <summary>
    /// RequestExtensions
    /// </summary>
    public static class RequestExtensions
    {
        /// <summary>
        /// 获取客户端IPV4 IP地址
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <returns>IP地址</returns>
        public static string GetClinetIP(this HttpRequestBase request)
        {
            string ip = string.Empty;
            ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(ip))
            {
                ip = request.ServerVariables["REMOTE_ADDR"];
            }

            if (string.IsNullOrEmpty(ip))
            {
                ip = request.UserHostAddress;
            }

            if (string.IsNullOrEmpty(ip))
            {
                return "127.0.0.1";
            }

            return ip;
        }

        /// <summary>
        /// 判断是否Post请求
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <returns>
        /// 是否Post请求
        /// </returns>
        public static bool IsPost(this HttpRequestBase request)
        {
            return request.HttpMethod.Equals("POST");
        }

        /// <summary>
        /// 判断是否Get请求
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <returns>
        /// 是否Get请求
        /// </returns>
        public static bool IsGet(this HttpRequestBase request)
        {
            return request.HttpMethod.Equals("GET");
        }

        /// <summary>
        /// 获得指定表单参数的值,如果为空则设置为缺省值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">表单参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>表单参数的值</returns>
        public static string GetFormString(this HttpRequestBase request, string key, string defaultValue)
        {
            if (string.IsNullOrEmpty(request.Form[key]))
            {
                return defaultValue;
            }

            return request.Form[key];
        }

        /// <summary>
        /// 获得指定表单参数的值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">表单参数</param>
        /// <returns>表单参数的值</returns>
        public static string GetFormString(this HttpRequestBase request, string key)
        {
            return GetFormString(request, key, string.Empty);
        }

        /// <summary>
        /// 获得指定Url参数的值,如果为空则设置为缺省值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">Url参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>Url参数的值</returns>
        public static string GetQueryString(this HttpRequestBase request, string key, string defaultValue)
        {
            if (string.IsNullOrEmpty(request.QueryString[key]))
            {
                return defaultValue;
            }

            return request.QueryString[key];
        }

        /// <summary>
        /// 获得指定Url参数的值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">Url参数</param>
        /// <returns>
        /// Url参数的值
        /// </returns>
        public static string GetQueryString(this HttpRequestBase request, string key)
        {
            return GetQueryString(request, key, string.Empty);
        }

        /// <summary>
        /// 获得指定Url参数或者指定表单参数的值,如果为空则设置为缺省值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>参数的值</returns>
        public static string GetString(this HttpRequestBase request, string key, string defaultValue)
        {
            string tmp = GetQueryString(request, key, defaultValue);
            if (string.IsNullOrEmpty(tmp))
            {
                return GetFormString(request, key, defaultValue);
            }
            else
            {
                return tmp;
            }
        }

        /// <summary>
        /// 获得指定Url参数或者指定表单参数的值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">参数</param>
        /// <returns>参数的值</returns>
        public static string GetString(this HttpRequestBase request, string key)
        {
            return GetString(request, key, string.Empty);
        }

        /// <summary>
        /// 获得指定表单参数的int类型值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">表单参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>表单参数的int类型值</returns>
        public static int GetFormInt(this HttpRequestBase request, string key, int defaultValue)
        {
            int result;
            if (int.TryParse(request.Form[key], out result))
            {
                return result;
            }

            return defaultValue;
        }

        /// <summary>
        /// 获得指定Url参数的int类型值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">Url参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>表单参数的int类型值</returns>
        public static int GetQueryInt(this HttpRequestBase request, string key, int defaultValue)
        {
            int result;
            if (int.TryParse(request.QueryString[key], out result))
            {
                return result;
            }

            return defaultValue;
        }

        /// <summary>
        /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>参数的int类型值</returns>
        public static int GetInt(this HttpRequestBase request, string key, int defaultValue)
        {
            int tmp = GetQueryInt(request, key, defaultValue);
            if (tmp == defaultValue)
            {
                return GetFormInt(request, key, defaultValue);
            }
            else
            {
                return tmp;
            }
        }

        /// <summary>
        /// 获得指定表单参数的Float类型值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">表单参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>表单参数的Float类型值</returns>
        public static float GetFormFloat(this HttpRequestBase request, string key, float defaultValue)
        {
            float result;
            if (float.TryParse(request.Form[key], out result))
            {
                return result;
            }

            return defaultValue;
        }

        /// <summary>
        /// 获得指定Url参数的Float类型值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">Url参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>
        /// 表单参数的Float类型值
        /// </returns>
        public static float GetQueryFloat(this HttpRequestBase request, string key, float defaultValue)
        {
            float result;
            if (float.TryParse(request.QueryString[key], out result))
            {
                return result;
            }

            return defaultValue;
        }

        /// <summary>
        /// 获得指定Url或表单参数的Float类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="request">HttpRequestBase</param>
        /// <param name="key">参数</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns>参数的Float类型值</returns>
        public static float GetFloat(this HttpRequestBase request, string key, float defaultValue)
        {
            float tmp = GetQueryFloat(request, key, defaultValue);
            if (tmp == defaultValue)
            {
                return GetFormFloat(request, key, defaultValue);
            }
            else
            {
                return tmp;
            }
        }
    }
}